/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.Kotxeak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import dBasea.Bezeroa;
import dBasea.Kotxea;

public class KotxeaBezeroa extends Panela {

	private static final long serialVersionUID = -9213003021678544713L;
	private JComponent zenbakia;
	private JTextField nif;
	private JButton ok;
	private JButton ezeztatu;
	private JLabel matri;
	private JTextField matrikula;
	private JFormattedTextField urte;
	private JLabel urtea;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private Bezeroa beze = new Bezeroa();
	private Kotxea kotx=new Kotxea();

	public KotxeaBezeroa(String tituloa, final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);
		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel geg = new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);
		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia = new JLabel("Bezero NAN:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		nif = new JTextField(8);
		nif.addKeyListener(new KeyAdapter() {// limitea NAN

			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length() >= 10) {
					arg0.consume();
				}
			}
		});
		arri.add(zenbakia);
		arri.add(nif);
		erdikoa.add(arri);

		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		arri.setBackground(SystemColor.activeCaption);

		btnNewButton = new JButton("Bilatu");
		btnNewButton.setIcon(new ImageIcon(KotxeaBezeroa.class
				.getResource("/Argazkia/lup.png")));
		arri.add(btnNewButton);
		FlowLayout arrib = new FlowLayout();
		arrib.setHgap(15);
		arrib.setVgap(10);
		arribb.setLayout(arrib);
		matrikula = new JTextField(15);
		matrikula.addKeyListener(new KeyAdapter() {// limitea NAN

					public void keyTyped(KeyEvent arg0) {
						if (matrikula.getText().length() >= 8) {
							arg0.consume();
						}
					}
				});
		matri = new JLabel("Kotxe matrikula:");
		arribb.add(matri);
		matri.setFont(new Font("Arial", Font.PLAIN, 14));
		arribb.add(matrikula);
		erdikoa.add(arribb);

		JPanel arr = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);

		btnNewButton_1 = new JButton("Bilatu");
		btnNewButton_1.setIcon(new ImageIcon(KotxeaBezeroa.class
				.getResource("/Argazkia/lup.png")));

		arribb.add(btnNewButton_1);
		arr.setBackground(SystemColor.activeCaption);
		FlowLayout rib = new FlowLayout();
		rib.setHgap(15);
		rib.setVgap(10);
		arr.setLayout(rib);
		try {
			urte = new JFormattedTextField(new MaskFormatter("##/##/####"));
			urte.setColumns(10);
		} catch (ParseException e1) {
			JOptionPane.showMessageDialog(null, "Txarto egin da bihurketa",
					"Oker", JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
		}
		urtea = new JLabel("Urtea (ee/hh/uuuu):");
		arr.add(urtea);
		urtea.setFont(new Font("Arial", Font.PLAIN, 14));
		arr.add(urte);
		erdikoa.add(arr);
		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Aldaketak egin");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		matrikula.setEnabled(false);
		urte.setEnabled(false);
		ok.setEnabled(false);

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!nifIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!beze.existitzenDa(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Bezero hau ez da existitzen\n"
										+ "Sartu bezero baten NAN-a", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {

						matrikula.setEnabled(true);
						nif.setEnabled(false);
						ok.setEnabled(true);
						JOptionPane.showMessageDialog(null, "Badago bezeroa!",
								"", JOptionPane.INFORMATION_MESSAGE);
					}
				}

				catch (Exception nfe) {
					JOptionPane.showMessageDialog(null,
							"Ezin izan da aurkitu.", "Oker",
							JOptionPane.ERROR_MESSAGE);
				}

			}
		});
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!matrikulaIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!kotx.existitzenDaMatrikula(matrikula.getText())) {
						JOptionPane.showMessageDialog(null,
								"Matrikula hau ez da existitzen\n"
										+ "Sartu matrikula berria", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {

						matrikula.setEnabled(false);
						urte.setEnabled(true);
						JOptionPane.showMessageDialog(null,
								"Badago matrikula!", "",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}

				catch (Exception nfe) {
					JOptionPane.showMessageDialog(null,
							"Ezin izan da aurkitu!", "Oker",
							JOptionPane.ERROR_MESSAGE);
					nfe.printStackTrace();
				}

			}
		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!zerbaiIdatzi()) {
					JOptionPane.showMessageDialog(null,
							"Sartu behar duzu zerbait zuriunetan\n"
									+ "Ezin da eremu hutsik egon", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else
					try {
						if (beze.existitzenDaMatrikulaBezeroarekin(matrikula.getText(),nif.getText())
								&& beze.existitzenDa(nif.getText())) {
							JOptionPane
									.showMessageDialog(
											null,
											"Kotxe hau existitzen da bezero batekin\n"
													+ "Sartu matrikula berria duen kotxea eta bezeroa",
											"Oker", JOptionPane.ERROR_MESSAGE);
						} else {
							try {
								SimpleDateFormat sdf = new SimpleDateFormat(
										"dd/MM/yyyy");
								long dataf = sdf.parse(urte.getText())
										.getTime();
								kotx.kotxeBezeroaSortu(matrikula.getText(),
										nif.getText(), dataf);
								JOptionPane.showMessageDialog(null,
										"Ondo sortu da Bezero-kotxe erlazioa",
										"", JOptionPane.INFORMATION_MESSAGE);
								eremuakGarbitu();
								matrikula.setEnabled(false);
								nif.setEnabled(true);
								urte.setEnabled(false);
								ok.setEnabled(false);
							} catch (Exception nfe) {
								JOptionPane
										.showMessageDialog(
												null,
												"Ezin izan da erlazioa burutu. Zuriuneak kendu edo sartu data",
												"Oker",
												JOptionPane.ERROR_MESSAGE);
							}
						}
					} catch (HeadlessException e) {
						JOptionPane
								.showMessageDialog(
										null,
										"Saiatu berriro!\n"
												+ "Sartu matrikula berria duen kotxea eta bezeroa",
										"Oker", JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					} catch (SQLException e) {
						JOptionPane
								.showMessageDialog(
										null,
										"Kotxe hau existitzen da bezero batekin\n"
												+ "Sartu matrikula berria duen kotxea eta bezeroa",
										"Oker", JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					}

			}
		});

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
				eremuakGarbitu();
				matrikula.setEnabled(false);
				nif.setEnabled(true);
				urte.setEnabled(false);
				ok.setEnabled(false);
			}
		});
	}

	@Override
	public void eremuakGarbitu() {
		nif.setText("");
		matrikula.setText("");
		urte.setText("");

	}

	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki = urte.getText().length() > 0;
		return aurki;
	}

	private boolean nifIdatzi() {
		boolean aurki;
		aurki=nif.getText().length() > 0;
   		return aurki;
   	}
	private boolean matrikulaIdatzi() {
		boolean aurki;
		aurki=matrikula.getText().length() > 0;
   		return aurki;
   	}
	}


